package com.example.design.b.composite.demo;

import java.util.Objects;

/**
 * 树叶构件
 * 商品
 */
public class Item implements Component {

    public Item(String name, float quantity, double price) {
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }

    /**
     * 商品名称
     */
    protected String name;
    /**
     * 商品数量
     */
    protected float quantity;
    /**
     * 商品价格
     */
    protected double price;

    @Override
    public double cal() {
        return quantity * price;
    }

    @Override
    public void show() {
        System.out.println("商品名称: " + name + ", 数量: " + quantity + ", 单价: " + price + ", 共: " + cal() + "元");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Item item = (Item) o;
        return quantity == item.quantity &&
                Double.compare(item.price, price) == 0 &&
                Objects.equals(name, item.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, quantity, price);
    }

    /* 因为该类主要是负责商品的, 所以用不到添加删除的逻辑, 所以这里没写 start */

    @Override
    public void add(Component component) {
    }

    @Override
    public void remove(Component component) {
    }
    /* 因为该类主要是负责商品的, 所以用不到添加删除的逻辑, 所以这里没写 end */
}
